package cn.iocoder.yudao.module.wine.controller.app.stockstore;

import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.module.wine.controller.app.stockstore.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.stockstore.StockStoreDO;
import cn.iocoder.yudao.module.wine.service.stockstore.StockStoreService;

@Tag(name = "用户 APP - 仓库信息")
@RestController
@RequestMapping("/wine/stock-store")
@Validated
public class AppStockStoreController {

    @Resource
    private StockStoreService stockStoreService;

    @PostMapping("/create")
    @Operation(summary = "创建仓库信息")
    public CommonResult<Long> createStockStore(@Valid @RequestBody AppStockStoreSaveReqVO createReqVO) {
        return success(stockStoreService.appCreateStockStore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "编辑仓库信息")
    public CommonResult<Boolean> updateStockStore(@Valid @RequestBody AppStockStoreSaveReqVO updateReqVO) {
        return success(stockStoreService.appUpdateStockStore(updateReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仓库信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteStockStore(@RequestParam("id") Long id) {
        stockStoreService.deleteStockStore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仓库信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppStockStoreRespVO> getStockStore(@RequestParam("id") Long id) {
        StockStoreDO stockStore = stockStoreService.getStockStore(id);
        AppStockStoreRespVO bean = BeanUtils.toBean(stockStore, AppStockStoreRespVO.class);
        bean.setAreaName(AreaUtils.format(stockStore.getAreaId(), " "));
        return success(bean);
    }

    @GetMapping("/list")
    @Operation(summary = "获得仓库信息列表")
    public CommonResult<List<AppStockStoreRespVO>> getStockStoreList(@Valid AppStockStoreListReqVO pageReqVO) {
        List<StockStoreDO> list = stockStoreService.getStockStoreList(pageReqVO);
        List<AppStockStoreRespVO> bean = BeanUtils.toBean(list, AppStockStoreRespVO.class);
        List<AppStockStoreRespVO> collect = bean.stream().peek(item -> {
            item.setAreaName(AreaUtils.format(item.getAreaId(), " "));
        }).collect(Collectors.toList());
        return success(collect);
    }
}
